<template>
  <view>
    <!-- 搜索框 -->

    <!-- 分类选择 -->
    <view class="scroll-view-container">
      <!-- 左侧一级分类 -->
      <!-- 左侧一级分类 -->
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh+'px'}">
        <view :class="['left-scroll-view-item', i===active ? 'active':'']" v-for="(item, i) in cateList" :key="i"
          @click="changeActive(i)">
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右侧二级分类 -->
      <scroll-view 
      class="right-scroll-view" 
      scroll-y="true" 
      :style="{height: wh+'px'}"
       :scroll-top="scrollTop"
      >
        <!-- 二级分类item -->
        <view class="right-scroll-view-item" v-for="(item, i) in cateLevel2" :key="i">
          <!-- 二级分类title -->
          <view class="right-scroll-view-item-title">
            /{{item.cat_name}}/
          </view>
          <!-- 二级分类内容容器 -->
          <view class="right-scroll-view-item-content">
            <!-- 二级分类每一项内容 -->
            <view 
            class="right-scroll-view-item-content-box" 
            v-for="(item2, i2) in item.children" 
            :key="i2"
            @click="gotoGoodsList(item2)"
            >
              <!-- 二级分类图片 -->
              <image :src="item2.cat_icon" mode="widthFix"></image>
              <!-- 二级分类图片 -->
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        wh: 0,
        // 分类数据列表
        cateList: [],
        // 左侧一级分类选中索引
        active: 0,
        // 二级分类列表
        cateLevel2: [],
        // 滚动条距离顶部的距离
        scrollTop: 0
      }
    },
    onLoad() {
      // 获取页面可用高度
      // https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync
      const sysInfo = uni.getSystemInfoSync()
      // 为wh赋值(windowHeight, 可使用窗口高度)
      this.wh = sysInfo.windowHeight
      // 获取数据
      this.getCateList()
    },
    methods: {
      // 点击二级分类详情跳转到商品列表页
      gotoGoodsList(goods){
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
        })
      },
      // 改变一级分类索引
      changeActive(i) {
      //  更新选中的索引
        this.active = i
        // 更新二级分类列表数据
        this.cateLevel2 = this.cateList[i].children
        // 更新右侧二级分类距离顶部距离
          this.scrollTop = Math.random()
      },
      // 获取分类数据
      async getCateList() {
        // 发起请求
        const {
          data: res
        } = await uni.$http.get('/categories')
        console.log(res)
        // 数据获取异常判断
        if (res.meta.status !== 200) return uni.$showMsg()
        // 保存数据到data中
        this.cateList = res.message
        // 为二级分类赋值
        this.cateLevel2 = res.message[0].children
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 240rpx;

      .left-scroll-view-item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 120rpx;
        background-color: #efefef;
        font-size: 24rpx;

        // 选中样式
        &.active {
          display: flex;
          justify-content: space-between;
          background-color: #ffffff;

          // 左边红线
          &::before {
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #c00000;
          }

          // 右边白线和背景相同
          &::after {
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #fffff;
          }
        }
      }
    }

    .right-scroll-view {
      .right-scroll-view-item {
        .right-scroll-view-item-title {
          display: flex;
          justify-content: center;
          font-weight: bold;
          font-size: 24rpx;
          padding: 20rpx 0;
        }

        .right-scroll-view-item-content {
          display: flex;
          flex-wrap: wrap;

          .right-scroll-view-item-content-box {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 33.33%;
            padding: 20rpx;
            box-sizing: border-box;

            image {
              width: 100%;
            }

            text {
              font-size: 24rpx;
            }
          }
        }
      }
    }
  }
</style>
